<template>
	<view v-if="loaded" class="container">
		<empty
			v-if="!addressList.length"
			:src="baseTemplateFileUri + 'image/emptyStatus/common.png'"
			desc="您还未添加收货地址"
		></empty>

		<view
			v-for="(item, index) in addressList"
			:key="index"
			@click="checkAddress(item)"
			class="page-panel address-wrap"
		>
			<view class="address-content">
				<view class="clamp title-box">
					<text class="title">{{ item.customerName }}</text>
					<text class="phone">{{ item.phone }}</text>
				</view>
				<view class="address-detail">
					{{ item.address }}{{ item.area }}
				</view>
			</view>
			<button
				class="btn"
				:style="themeStyles.btnAuxiliaryColor"
				@click.stop="operateAddress('edit', item)"
			>
				编辑
			</button>
		</view>

		<view class="footer-wrap">
			<button
				:style="themeStyles.btnFullColor"
				class="add-btn"
				@click="operateAddress('add')"
			>
				新增地址
			</button>
		</view>
	</view>
</template>

<script>
import { queryAddressList } from "@/lib/api/customerServer/address";
import empty from "@/components/empty";
let currentPage = 1;
export default {
	components: {
		empty
	},
	data() {
		return {
			loadingType: "more",
			source: null, // 1： 选择地址; 2: 修改订单地址
			addressList: []
		};
	},
	onLoad(option) {
		currentPage = 1;
		console.log(option.source);
		this.source = option.source;
	},

	//下拉刷新
	async onPullDownRefresh() {
		currentPage = 1;
		await this.getAddrInfos();
		uni.stopPullDownRefresh();
	},

	onReachBottom() {
		if (this.loadingType === "more") {
			currentPage++;
			this.getAddrInfos();
		}
	},

	onShow() {
		this.getAddrInfos();
	},

	methods: {
		async getAddrInfos() {
			const propsData = {
				currentPage
			};
			try {
				const res = await queryAddressList();
				if (propsData.currentPage === 1) {
					this.addressList = res.data.list;
				} else {
					this.addressList = this.addressList.concat(res.data.list);
				}
				const noMore = this.addressList.length >= res.data.total;
				this.loadingType = noMore ? "noMore" : "more";
				this.loaded = true;
			} catch (error) {
				console.log("error", error);
				await this.$api.showToast(error.resultInfo || "加载失败");
				uni.navigateBack();
			}
		},

		//选择地址
		checkAddress(item) {
			console.log("item", item);
			if (this.source === "1") {
				//this.$api.prePage() 获取上一页实例，在App.vue定义
				this.$api.prePage().addressData = item;
				uni.navigateBack();
				// this.$api
				//   .prePage()
				//   .onTallyOrder()
				//   .then(res => {
				//     uni.navigateBack();
				//   });
			} else if (this.source === "2") {
				//this.$api.prePage()获取上一页实例，在App.vue定义
				// this.$api
				//   .prePage()
				//   .onUpdateOrderAddrFromApp()
				//   .then(res => {
				//     uni.navigateBack();
				//   })
				//   .catch(error => {
				//     console.log("error", error);
				//   });
			} else {
				this.operateAddress("edit", item);
			}
		},

		operateAddress(type, item) {
			uni.navigateTo({
				url: `/pages/user/pages/address/addressDetail?type=${type}&data=${JSON.stringify(
					item
				)}`
			});
		},

		//添加或修改成功之后回调
		refreshList(data, type) {
			//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
			this.addressList.unshift(data);

			console.log(data, type);
		}
	}
};
</script>

<style lang='scss'>
@import "@/pages/user/styles/address/addressList.scss";
</style>
